package graph.editor;

import graph.util.AutomatonInfo;
import graph.util.XMLReaderWriter;

import java.awt.Dimension;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class GraphEditor implements FramesController {
	
	public final static String MENU_FILE = "File";
	public final static String MENU_ITEM_NEW = "New";
	public final static String MENU_ITEM_CLOSE = "Close";
	public final static String MENU_ITEM_QUIT = "Quit";
	
	public static final String MENU_ITEM_OPEN = "Open";
	public static final String MENU_ITEM_SAVE = "Save";
	public static final String MENU_AUTOMATON = "Automaton";
	public static final String MENU_ITEM_DRAW = "Draw automatically";
	public static final String MENU_ITEM_VISUALIZE = "Visualize";

	public final static String DIALOG_QUIT_MSG = "Do you really want to quit ?";
	public final static String DIALOG_QUIT_TITLE = "Quit ?";

	public final static String TITLE = "Graph";

	private static final List<JFrame> frames = new ArrayList<JFrame>();

	public void quit() {
		int answer = JOptionPane.showConfirmDialog(null, DIALOG_QUIT_MSG,
				DIALOG_QUIT_TITLE, JOptionPane.YES_NO_OPTION);
		if (answer == JOptionPane.YES_OPTION) {
			System.exit(0);
		}
	}

	public JFrame createFrame() {
		JFrame frame = new GraphFrame(this);
		frame.setTitle(TITLE);
		int pos = 30 * (frames.size() % 5);
		frame.setLocation(pos, pos);
		frame.setPreferredSize(new Dimension(700, 800));
		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
		frames.add(frame);
		return frame;
	}

	public void deleteFrame(JFrame frame) {
		if (frames.size() > 1) {
			frames.remove(frame);
			frame.dispose();
		} else {
			quit();
		}
	}
	
	/* Added by Bien Nguyen */
	public void open() {
		JFileChooser fileChooser = new JFileChooser();
		FileNameExtensionFilter filter = 
			new FileNameExtensionFilter("eXtensible Markup Language file (.xml)", "xml", "XML");
		fileChooser.setFileFilter(filter);

		int returnVal = fileChooser.showOpenDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String fileName = fileChooser.getSelectedFile().getName();
			int dotIndex = fileName.lastIndexOf(".");
			String extension = fileName.substring(dotIndex);
			if (extension.equalsIgnoreCase(".xml")) {
				String filePath = fileChooser.getSelectedFile().getPath();
				XMLReaderWriter rw = new XMLReaderWriter();
				rw.readXMLFile(filePath); // Read xml file
				rw.printAutomaton();
			} else {
				JOptionPane.showMessageDialog(fileChooser, "You must choose a xml file");
			}
		}
	}
	
	public void save(AutomatonInfo info) {
		if (info == null) {
			JOptionPane.showMessageDialog(null, "There is no Automaton information to save");
			return;
		}
		JFileChooser fileChooser = new JFileChooser();
		FileNameExtensionFilter filter = 
			new FileNameExtensionFilter("eXtensible Markup Language file (.xml)", "xml", "XML");
		fileChooser.setFileFilter(filter);
		
		int returnVal = fileChooser.showSaveDialog(null);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			String fileName = fileChooser.getSelectedFile().getName();
			String filePath;
			int dotIndex = fileName.lastIndexOf(".");
			if (dotIndex != -1) { // the fileName contains "."
				String extension = fileName.substring(dotIndex);
				if (extension.equalsIgnoreCase(".xml")) {
					filePath = fileChooser.getSelectedFile().getPath();
				} else {
					filePath = fileChooser.getSelectedFile().getPath() + ".xml";
				}
			} else { // the fileName does not contain "."
				filePath = fileChooser.getSelectedFile().getPath() + ".xml";
			}
			XMLReaderWriter rw = new XMLReaderWriter();
			rw.setInfo(info);
			rw.writeXMLFile(filePath); // Write Automaton information to xml file
		}
	}
	/* End: Added by Bien Nguyen */

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new GraphEditor().createFrame();
			}
		});
	}
}